package com.mopeso.Utils;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.cash.lemoloan.jsbridge.JsBridgeApplication;
import com.mopeso.MyApplication;

import java.util.ArrayList;
import java.util.List;


public class PermissionUtil {
    public static final int DEFAULT_REQUEST_CODE = 1;
    private String[] mPermissions;
    private OnPermissionCallback mPermissionCallback;
    private List<String> onGranted;
    private List<String> onDenied;
    private static PermissionUtil permissionUtil;
    private static Activity mActivitys;

    public static PermissionUtil getInstance(@NonNull Activity activity){
        if(permissionUtil == null){
            permissionUtil = new PermissionUtil();
        }
        mActivitys = activity;
        return permissionUtil;
    }

    public void requestPermissions(@NonNull String[] permissions, int requestCode, @NonNull OnPermissionCallback callback) {
        if (permissions.length < 1) {
            return;
        }
        onGranted = new ArrayList<String>();
        onDenied = new ArrayList<String>();
        mPermissions = permissions;
        mPermissionCallback = callback;
        for (String permission : permissions) {
            if(checkPermissions(permission)){
                onGranted.add(permission);
            }else {
                onDenied.add(permission);
            }
        }
        if(onDenied.size() != 0){
            String[] array = onDenied.toArray(new String[onDenied.size()]);
            onDenied.clear();
            requestPermission(array,requestCode);
        }else {
            if(mPermissionCallback != null){
                JsBridgeApplication.getMainHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPermissionCallback.onGranted(requestCode,onGranted);
                        mPermissionCallback.onDenied(requestCode,onDenied);
                    }
                },300);

            }
        }

    }
    public boolean checkPermissions(String permission){
        return ActivityCompat.checkSelfPermission(mActivitys,permission) == PackageManager.PERMISSION_GRANTED;
    }
    public static boolean checkPermissionsGranted(String... permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(MyApplication.getApplication(), permission) !=
                    PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }
    private void requestPermission(String[] permissions,int requestCode){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mActivitys.requestPermissions(permissions,requestCode);
        }else{
            ActivityCompat.requestPermissions(mActivitys,permissions,requestCode);
        }
    }

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(onGranted != null && onDenied !=null){
            for (int i = 0; i < grantResults.length; i++) {
                if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
                    onGranted.add(permissions[i]);
                }else {
                    onDenied.add(permissions[i]);
                }
            }
        }
        if(mPermissionCallback != null){
            if(mPermissionCallback != null){
                JsBridgeApplication.getMainHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPermissionCallback.onGranted(requestCode,onGranted);
                        mPermissionCallback.onDenied(requestCode,onDenied);
                    }
                },500);

            }

        }
    }
    public boolean shouldShowRequestPermissionRationale(@NonNull String permission){
        return ActivityCompat.shouldShowRequestPermissionRationale(mActivitys,permission);
    }
    public interface OnPermissionCallback {
        // 授权；
        void onGranted(int requestCode,List<String>list);

        // 拒绝；
        void onDenied(int requestCode,List<String>list);
    }

}
